package com.apes.scm.masterdata.frame.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
//import com.apes.scm.rbac.model.Post;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 功能：行政部门
 *
 * @author HuJianlin
 * @create 2018-03-17 16:00
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "mas_branch")
public class Branch extends Model implements Comparable<Branch> {

    @Id
    @Column(name = "id", length = 30)
    @Comment("部门编码")
    @ApesGenerator(code = "mas_branch_sequence", name = "行政部门",padding = 8)
    private String id;  //部门编码

    @Name
    @Column(name = "name", nullable = false, length = 120)
    @Comment("部门名称")
    private String name; //部门名称

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parents_id", referencedColumnName = "id")
    @Comment("上级部门编码")
    private Branch parents;  //上级部门编码

    @JSONField(serialize = false)
    @OneToMany(mappedBy = "parents", fetch = FetchType.LAZY)
    private Set<Branch> children;

    @Column(name = "levels", nullable = false, length = 120)
    @Comment("级别")
    private int levels; //级别

    @Column(name = "is_last_stage", nullable = false)
    @Comment("末级标记")
    private boolean lastStage = false;  //末级标记


    @Column(name = "ORIGIN", length = 20)
    @Comment("外部ID")
    private String origin; //外部ID

    @Column(name = "valid", nullable = false, columnDefinition = "number(1,0) default 1")
    @Comment("有效")
    private boolean valid = true; //有效

    @Name
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_type", referencedColumnName = "id")
    private Enumeration branchType; //部门类型（businessClass）（BC_purchase：采购; BC_sale：销售; BC_other：其他; BC_company: 公司）

//    @JSONField(serialize = false)
//    @OneToMany(fetch = FetchType.LAZY, mappedBy = "branch")
//    private Set<Post> posts;

    public Set<Branch> findBrother() {
        if (parents == null) return null;
        return parents.getChildren().stream().filter(branch -> !branch.equals(this)).collect(Collectors.toSet());
    }

    public Set<Branch> findBrotherAndSelf() {
        if (parents == null) {
            Set<Branch> result = new HashSet<>();
            result.add(this);
            return result;
        }
        return parents.getChildren();
    }

    /***
     * 获取当前节点的所有父节点（ 根节点 --> 当前节点 ）
     * @return
     */
//    public List<Branch> findParents() {
//        List<Branch> cacheList;
//        if(this.getParents() != null) {
//            cacheList = this.getParents().findParents();
//        } else {
//            cacheList = new ArrayList();
//        }
//        cacheList.add(this);
//        return cacheList;
//    }

    @Transient
    private String path;

    public String getPath() {
        String path = null;
        if (parents != null) path = parents.getPath();
        return path != null ? path + "/" + id : id;
    }


    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof Branch)) return false;
        return (this.getId() == ((Branch) obj).getId());
    }

    @Override
    public int compareTo(Branch branch) {
        if(this.id == branch.id){
            return 1;
        }
        return 0;
    }

    @Column(name = "open_day")
    @Comment("开业时间")
    private Date openDay;//开业时间

    @Column(name = "abbreviation",length = 120)
    @Comment("简称")
    private String abbreviation;//简称
}
